import { mapGetters, mapActions } from "vuex";
import Clipboard from "clipboard";
import Qrcode from "vux/src/components/qrcode/index.vue";

import template from "./paymentTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  data() {
    return {
      amountVal: 0,
      code: "",
      order: "",
      codeUrl: "",
      codeImg: "",
      collectBank: 0,

      returnPopup: false,
      size: 135,
      codeShow: false,
    };
  },
  components: {
    Qrcode,
  },
  created() {
    this.$nextTick(() => {
      if (!this.rechargeData.payment_name) {
        this.$router.back();
      }
    });

    this.rechargeData.prompt = this.rechargeData.prompt;
    this.code = this.$route.query.code;
    this.order = this.$route.query.order;
    this.amountVal = this.$route.query.amountVal;

    this.codeUrl = this.$route.query.codeUrl;
    this.codeImg = this.$route.query.codeImg;
    this.collectBank = this.$route.query.collectBank;
  },
  mounted() {},
  computed: {
    ...mapGetters(["rechargeData"]),
  },
  methods: {
    onConfirm() {
      this.$router.back();
    },
    headBack() {
      this.returnPopup = true;
    },
    copyText() {
      var clipboard = new Clipboard(".icoCopy");
      clipboard.on("success", (e) => {
        this.$vux.toast.show({
          text: "复制成功",
        });
        clipboard.destroy();
      });
      clipboard.on("error", (e) => {
        this.$vux.toast.show({
          text: "该浏览器不支持自动复制",
        });
        clipboard.destroy();
      });
    },
    submitOnLine(num) {
      if (this.$router.currentRoute.name != "Payment") {
        clearInterval(this.timer);
        return;
      }
      var params = {
        token: localStorage.getItem("token"),
        order_no: this.order,
      };
      if (num != "2") {
        window.open(
          this.rechargeData.channel_type == "ali"
            ? "alipays://"
            : this.rechargeData.channel_type == "wx"
            ? "weixin://"
            : this.rechargeData.channel_type == "qq"
            ? "mqqapi://"
            : "",
          "_self"
        );
      }

      clearInterval(this.timer);
      this.$http
        .post(this.urlRequest + "?m=api&c=recharge&a=getRechargeInfo", params)
        .then((res) => {
          if (res.status == 0) {
            setTimeout(() => {
              if (res.state == 0) {
                this.timer = setInterval(() => {
                  this.submitOnLine(2);
                }, 3000);
                return;
              }
              if (res.state == 1) {
                this.$router.replace({
                  path: "/recharge/payStatus",
                  query: {
                    order: this.order,
                    code: this.code,
                    amountVal: this.amountVal,
                  },
                });
              }
            }, 3000);
          }
        })
        .catch((error) => {
          this.timer = setInterval(() => {
            this.submitOnLine();
          }, 3000);
          this.$vux.loading.hide();
          this.$vux.toast.show({
            text: "数据请求超时",
          });
        });
    },
    submitOffLine() {
      this.$vux.loading.show();
      var params = {
        token: localStorage.getItem("token"),
        order_sn: this.order,
      };
      this.$http
        .post(this.urlRequest + "?m=api&c=recharge&a=setRechargeMusic", params)
        .then((res) => {
          this.$vux.loading.hide();
          if (res.status == 0) {
            this.$router.replace({
              path: "/recharge/payStatus",
              query: {
                order: this.order,
                code: this.code,
                amountVal: this.amountVal,
              },
            });
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              this.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
        })
        .catch((error) => {
          this.$vux.loading.hide();
          this.$vux.toast.show({
            text: "数据请求超时",
          });
        });
    },
  },
  watch: {},
  deactivated() {
    clearInterval(this.timer);
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
};

// WEBPACK FOOTER //
// ./src/components/wallet/recharge/payment.vue
